package HLLib.userControl.HLPanelPickUp;

import javax.microedition.lcdui.Image;

import HLLib.base.HLGraphics;
import HLLib.base.HLGraphics_H;
import HLLib.base.HLImage;
import HLLib.base.HLString;
import HLLib.control.HLPanel;
import HLLib.control.HLPhotoFrame;
import HLLib.control.HLTextPageOld;
import HLLib.handfere.HLCommunityData.HLTrend.HLATrend;

public class HLTrendsPanel extends HLPanel {
    public static final int PHOTO_X = 3;

    public static final int PHOTO_Y = 10;

    public static final int PHOTO_TO_TEXT = 10;

    public static final int TEXT_Y = 2;

    public HLATrend trend;

    HLPhotoFrame photoFrame;

    HLPhotoFrame pic;

    int xText;

    String title;

    HLTextPageOld textPageBody;

    HLTextPageOld textPageTitle;

    public int selectColor = 0xffff00;

    public int color = 0xffecf3fd;

    Image img;

    private boolean needDownload;

    private boolean haveDownloadOver;

    private int oriHeight;

    public HLTrendsPanel(HLATrend diary, int width, Image img) {
        this.trend = diary;
        this.width = (short) width;
        this.img = img;
        this.backcolor = -1;
        this.dock = DOCK_STYLE_TOP;
        tabelStop = true;
        photoFrame = new HLPhotoFrame(diary.head);
        photoFrame.x = PHOTO_X;
        photoFrame.y = PHOTO_Y;
        Add(photoFrame);

        title = diary.title;

        xText = PHOTO_X + photoFrame.width + PHOTO_TO_TEXT;
        textPageTitle = new HLTextPageOld(HLLib.base.HLScreen.width - xText - 22);
        textPageTitle.textColor = 0xff2d82e2;
        try {

            textPageTitle.SetText1(diary.title);
        } catch (Exception e) {
            textPageTitle.SetText1(diary.title);
        }

        textPageBody = new HLTextPageOld(HLLib.base.HLScreen.width - xText - 22);
        textPageBody.textColor = 0xff4b4b4b;
        textPageBody.SetText1(diary.body);
        String tmp = "";
        this.height = (short) (TEXT_Y + textPageTitle.height + TEXT_Y + textPageBody.height);
        if (diary.img != null) {
            pic = new HLPhotoFrame(diary.img);
            pic.x = xText;
            pic.y = height + 2;
            Add(pic);
            this.height += pic.GetHeight() + 4;
            needDownload = true;
            oriHeight = diary.img.GetHeight();
        }
        this.height += HLGraphics.StringHeight() + 4;
    }

    public void Logic() {
        if (needDownload) {
            if (trend.img != null && !haveDownloadOver) {
                if (trend.img.GetImage1() == null) {
                    haveDownloadOver = false;
                } else {
                    haveDownloadOver = true;
                    this.height += trend.img.GetHeight() - oriHeight;
                    parent.AdjustSize();
                    GetUiRoot().Refresh();
                }
            }
        }
        super.Logic();
        if (IsFocus()) {
            RefreshEvent(GetScreenX(), GetScreenY(), GAME_BUTTON_ACTION);
        }
    }

    public void Refresh() {
        super.Refresh();

    }

    public void Render(HLGraphics g) {
        // super.Render(g);
        int screenX = GetScreenX();
        int screenY = GetScreenY();
        g.FillRect(screenX, screenY, width, height, 0xffffff);
        if (IsFocus()) {
            // g.DrawImageFillRect(new HLImage(img[1]), screenX, screenY, width,
            // img[1].getHeight(), HLGraphics_H.TRANS_NONE);
            // g.DrawImageFillRect(new HLImage(img[0]), screenX, screenY +
            // height - img[0].getHeight(), width, img[1].getHeight(),
            // HLGraphics_H.TRANS_NONE);
            g.FillRect(screenX, screenY, width, height, HLPanelGroup.SELECTED_COLOR);// d0d9f2);//0xd0ebf2);
            g.DrawImage(new HLImage(img), screenX + width - img.getWidth() - 5, screenY + (height - img.getHeight() >> 1), HLGraphics_H.TRANS_NONE);
        } else {

        }
        int offsetX = screenX + xText;
        int offsetY = screenY + TEXT_Y;

        textPageTitle.Render(g, offsetX, offsetY);

        offsetY += textPageTitle.height + TEXT_Y;
        textPageBody.Render(g, offsetX, offsetY);
        offsetY += textPageBody.height + 2;

        if (pic != null) {
            pic.Render(g);
            offsetY += pic.GetHeight() + 2;
        }
        g.DrawString(new HLString(trend.dateLine), offsetX, offsetY, 0xff646567, 0, TEXT_STYLE_NORMAL);
        //
        photoFrame.Render(g);

    }

}
